package com.zmn.plat.services.impl.servcateory;

import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.util.SimpleSpellUtil;
import com.zmn.plat.manager.interfaces.cache.CommonCache;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.entity.servcategory.ServCategoryQuery;
import com.zmn.plat.persistence.interfaces.servcategory.ServCategoryDao;
import com.zmn.plat.services.interfaces.servcategory.ServCategoryService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2018/10/23 14:58
 */
@Service
public class ServCategoryServiceImpl implements ServCategoryService {

    @Resource
    ServCategoryDao servCategoryDao;
    @Resource
    CommonCache commonCache;

    @Override
    public ServCategory findByKey(Integer categoryId) {
        return servCategoryDao.findByKey(categoryId);
    }

    @Override
    public ServCategory findFirstOneByCategIdAsc() {
        return servCategoryDao.findFirstOneByCategIdAsc();
    }

    @Override
    public List<ServCategory> findByKeys(List<Integer> categIds) {
        return servCategoryDao.findByKeys(categIds);
    }

    @Override
    public ServCategory findByName(String name) {
        return servCategoryDao.findByName(name);
    }

    @Override
    public List<ServCategory> listPageByQuery(ServCategoryQuery query) {
        int count = servCategoryDao.countByQuery(query);
        query.setTotalCount(count);
        return servCategoryDao.listPageByQuery(query);
    }

    @Override
    public List<ServCategory> listShowPageByQuery(ServCategoryQuery query) {
        int count = servCategoryDao.countByQueryForShow(query);
        query.setTotalCount(count);
        return servCategoryDao.listPageByQueryForShow(query);
    }

    @Override
    public List<ServCategory> listByQuery(ServCategory query) {
        return servCategoryDao.listByQuery(query);
    }

    @Cacheable(key = "'servCategory:all'", cacheNames = "redis1h")
    @Override
    public List<ServCategory> listServCategoryAll() {

        return servCategoryDao.listAll();
    }

    @Override
    public List<ServCategory> listByQueryForShow(ServCategory query) {
        return servCategoryDao.listByQueryForShow(query);
    }

    @Override
    public List<ServCategory> listServCategoryByChannelId(Integer channelId, Integer showType) {
        if (Objects.equals(showType, ProductConsts.ERP_PRODUCT_TYPE)) {
            return servCategoryDao.listServCategoryByChannelId(channelId, showType);
        } else {
            return servCategoryDao.listShowServCategoryByChannelId(channelId, showType);
        }
    }

    @Override
    public List<ServCategory> listServCategoryByChannelIdAndCityId(Integer channelId, Integer showType, Integer cityId) {
        return servCategoryDao.listServCategoryByChannelIdAndCityId(channelId, showType, cityId);
    }

    @Override
    public List<DictModel> listAll() {
        List<ServCategory> list = listByQuery(new ServCategory());
        List<DictModel> models = new ArrayList<>(list.size());

        list.forEach(categ -> {
            models.add(new DictModel(categ.getCategId(), categ.getName()));
        });
        return models;
    }

    @Override
    public Integer countByQuery(ServCategoryQuery query) {
        return servCategoryDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ServCategory category) {
        return servCategoryDao.updateByKey(category);
    }

    @Override
    public Integer updateFrontByKey(ServCategory category) {
        return servCategoryDao.updateFrontByKey(category);
    }

    @Override
    public Integer updateSortKey(int categId, int sort, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setSort(sort);
        category.setUpdater(updater);
        category.setUpdateTime(DateUtil.getNow());

        return servCategoryDao.updateSortKey(category);
    }

    @Override
    public Integer updateShowSortKey(int categId, int sort, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setShowSort(sort);
        category.setShowUpdater(updater);
        category.setShowUpdateTime(DateUtil.getNow());

        return servCategoryDao.updateShowSortKey(category);
    }

    @Override
    public Integer updateShowFirstLetterKey(int categId, String firstLetter, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setShowFirstLetter(firstLetter);
        category.setShowUpdater(updater);
        category.setShowUpdateTime(DateUtil.getNow());

        return servCategoryDao.updateShowFirstLetterKey(category);
    }

    @Override
    public Integer updateShowSimpleSpellByKey(Integer categId, String simpleSpell, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setShowSimpleSpell(simpleSpell);
        category.setShowUpdater(updater);
        category.setShowUpdateTime(DateUtil.getNow());

        return servCategoryDao.updateShowSimpleSpellByKey(category);
    }

    @Override
    public Integer updateSimpleSpellByKey(Integer categId, String simpleSpell, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setSimpleSpell(simpleSpell);
        category.setShowUpdater(updater);
        category.setShowUpdateTime(DateUtil.getNow());

        return servCategoryDao.updateSimpleSpellByKey(category);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> arrayIds, int status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(arrayIds);
        dto.setStatus(status);
        dto.setUpdater(updater);
        dto.setUpdateTime(DateUtil.getNow());
        clear(arrayIds);

        return servCategoryDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateShowStatusBatch(List<Integer> arrayIds, int status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(arrayIds);
        dto.setStatus(status);
        dto.setUpdater(updater);
        dto.setUpdateTime(DateUtil.getNow());
        clear(arrayIds);
        return servCategoryDao.updateShowStatusBatch(dto);
    }

    @Override
    public int updateNameKey(Integer categId, String name, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setName(name);
        category.setFirstLetter(InitialUtil.getFristEnName(name));
        category.setSimpleSpell(SimpleSpellUtil.parseNameToSimpleSpell(name,null));
        category.setUpdater(updater);
        category.setUpdateTime(DateUtil.getNow());
        clear(Arrays.asList(categId));
        return servCategoryDao.updateNameKey(category);
    }

    @Override
    public int updateShowNameKey(Integer categId, String name, String updater) {
        ServCategory category = new ServCategory();
        category.setCategId(categId);
        category.setShowName(name);
        category.setShowFirstLetter(InitialUtil.getFristEnName(name));
        category.setShowUpdater(updater);
        category.setShowUpdateTime(DateUtil.getNow());
        clear(Arrays.asList(categId));
        return servCategoryDao.updateShowNameKey(category);
    }

    @Override
    public Integer insert(ServCategory category) {
        int result = servCategoryDao.insert(category);
        commonCache.delByKey(CommonCache.PRERIX + "scateg:all");
        return result;
    }

    @Override
    public Integer deleteByKey(int categId) {
        clear(Arrays.asList(categId));
        return servCategoryDao.deleteByKey(categId);
    }

    private void clear(List<Integer> arrayIds) {
        commonCache.delByKey(CommonCache.PRERIX + "scateg:all");
        if (!CollectionUtils.isEmpty(arrayIds)) {
            arrayIds.forEach(e -> commonCache.delByKey(CommonCache.PRERIX + "scateg:id:" + e));
        }
    }
}
